<?php
class AdminUserAction extends Action {
	
	//主登录页面
	public function index(){
		$this->display('login');
	}
	
	//验证码
	public function verify(){
       Image::buildImageVerify(4,1,'png',51,22,'verify');
    }
    
	//管理员登录
    public function login() {   
        $verify = $_POST['verify'];
        $admin = M('adminuser');
        
        if ($_SESSION['verify'] != md5($verify)){
        	 $this->ajaxReturn($this->$ajaxData,"验证码输入错误",FALSE);
        }
        
        $condition = array('adminName' => $_POST['adminUser'],'password' => md5($_POST['password']));
        
        $data = $admin ->where($condition)->find();
        //echo $admin->getLastSql();
        if(empty($data)){
        	 $this->ajaxReturn($this->$ajaxData,"用户名或密码不正确",FALSE);
        }else{
        	//设置session
        	session('_admin',$data);           
        	$this->insertLoginLogs($data['id']);
        	$this->ajaxReturn($this->$ajaxData,"用户名或密码不正确",TRUE);
        }
    } 
    
	//管理员登录日志
	private function insertLoginLogs($adminId){
		$adminInfo = M('adminlogininfo');
		$ipLocal = new Iplocation();          //新建一个IP查询对象
		$client = new MyClient();             //客户端信息获取工具类
		
		$ip = get_client_ip();
		$date = array('adminId' => $adminId,
		              'ip' => $ip,
			          'local' => $ipLocal -> getlocationStr($ip),
			          'browser' => $client -> getBrowse(),
			          'userOs' => $client -> getUseOs(),
			          'datatime' => time()
        );
        
        $result = $adminInfo->add($date);    // 写入数据库    
	}
	

	private function insertAdmin(){
		$adminInfo = M('adminuser');
		
		$date = array(adminName => 'fbadmin',
		              password => md5('fb118432'),
			          sex => '男',
			          age => 24,
			          datatime => time()
        );
        
        $result = $adminInfo->add($date);    // 写入数据库    
        echo $result;
	}
	
}